<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名" prop="customerId">
        <el-input
            v-model="queryParams.realName"
            placeholder="请输入客户名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomId">
        <el-input
          v-model="queryParams.roomId"
          placeholder="请输入房间号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="入住时间" prop="checkinDate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.checkinDate"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择入住时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="退房时间" prop="checkoutDate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.checkoutDate"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择退房时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" style="width: 120px" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in sys_yuyue_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:hotel_reservation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:hotel_reservation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:hotel_reservation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:hotel_reservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hotel_reservationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名" align="center" prop="lhkRenzheng.realName" />
      <el-table-column label="联系方式" align="center" prop="lhkRenzheng.realPhone" />
      <el-table-column label="身份证号" show-overflow-tooltip align="center" prop="lhkRenzheng.sfzId" />
      <el-table-column label="入住酒店" align="center" prop="lhkRoom.hotelInfo.hotelName" />
      <el-table-column label="房间号" align="center" prop="lhkRoom.roomNumber" />
      <el-table-column label="入住时间" align="center" prop="checkinDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.checkinDate, '{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退房时间" align="center" prop="checkoutDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.checkoutDate, '{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="入住人数" align="center" prop="guestCount" />-->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_yuyue_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:hotel_reservation:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:hotel_reservation:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改酒店预约管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="hotel_reservationRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="!form.reservationId" label="客户id" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item v-if="!form.reservationId" label="房间类型" prop="roomTypeId">
          <el-input v-model="form.typeId" placeholder="请输入房间类型" />
        </el-form-item>
        <el-form-item v-if="form.reservationId" label="房间号" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="入住时间" prop="checkinDate">
          <el-date-picker clearable
            v-model="form.checkinDate"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择入住时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退房时间" prop="checkoutDate">
          <el-date-picker clearable
            v-model="form.checkoutDate"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择退房时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入住人数" prop="guestCount">
          <el-input v-model="form.guestCount" placeholder="请输入入住人数" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in sys_yuyue_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Hotel_reservation">
import { listHotel_reservation, getHotel_reservation, delHotel_reservation, addHotel_reservation, updateHotel_reservation } from "@/api/system/hotel_reservation"
import useUserStore from '@/store/modules/user'
import {parseTime} from "../../../utils/ruoyi.js";
import { listInfo } from "@/api/system/info"
const userStore = useUserStore();

const { proxy } = getCurrentInstance()
const { sys_yuyue_status } = proxy.useDict('sys_yuyue_status')

const hotel_reservationList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    customerId: null,
    hotelId: null,
    roomId: null,
    checkinDate: null,
    checkoutDate: null,
    roomCount: null,
    guestCount: null,
    status: null,
    managerId: null,
    dingdanId: null
  },
  rules: {
    customerId: [
      { required: true, message: "客户id不能为空", trigger: "blur" }
    ],
    hotelId: [
      { required: true, message: "酒店id不能为空", trigger: "blur" }
    ],
    roomId: [
      { required: true, message: "房间id不能为空", trigger: "blur" }
    ],
    checkinDate: [
      { required: true, message: "入住时间不能为空", trigger: "blur" }
    ],
    checkoutDate: [
      { required: true, message: "退房时间不能为空", trigger: "blur" }
    ],
    roomCount: [
      { required: true, message: "预定房间数量不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询酒店预约管理列表 */
function getList() {
  loading.value = true
  console.log(userStore.id)
  listHotel_reservation({...queryParams.value, managerId: userStore.id}).then(response => {
    console.log("---------------")
    console.log(response)
    hotel_reservationList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    reservationId: null,
    customerId: null,
    hotelId: null,
    roomId: null,
    checkinDate: null,
    checkoutDate: null,
    roomCount: null,
    guestCount: null,
    status: null,
    createTime: null,
    updateTime: null,
    managerId: null,
    dingdanId: null
  }
  proxy.resetForm("hotel_reservationRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.reservationId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加酒店预约管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _reservationId = row.reservationId || ids.value
  console.log(row.lhkRoom)
  getHotel_reservation(_reservationId).then(response => {
    form.value = response.data
    form.value.roomId = row.lhkRoom.roomNumber
    open.value = true
    title.value = "修改酒店预约管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hotel_reservationRef"].validate(valid => {
    if (valid) {
      if (form.value.reservationId != null) {
        updateHotel_reservation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        console.log(queryParams.value)
        console.log(form.value)
        addHotel_reservation({...form.value, managerId: userStore.id, hotelId: queryParams.value.hotelId}).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _reservationIds = row.reservationId || ids.value
  proxy.$modal.confirm('是否确认删除酒店预约管理编号为"' + _reservationIds + '"的数据项？').then(function() {
    return delHotel_reservation(_reservationIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/hotel_reservation/export', {
    ...queryParams.value
  }, `hotel_reservation_${new Date().getTime()}.xlsx`)
}

// 获取hotelId
let hotelId = ref(null)
function getHotelId() {
  listInfo({managerId: userStore.id}).then(response => {
    console.log(response.rows[0].hotelId)
    queryParams.value.hotelId = response.rows[0].hotelId
    // hotelId.value = response.data.hotelId
  })
}
onMounted(() => {
  getHotelId()
  getList()
})
</script>
